package http.secure.common.keys.impl;


import http.secure.common.keys.SecureGuidSession;
import http.secure.common.keys.SecureKeyStore;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

public record RedisKeyStore(RedisTemplate<String, String> template,
                            SecureGuidSession support) implements SecureKeyStore {

    @Override
    public void save(String key, String value, long timeout) {
        String fk = buildKey(key);
        template.opsForValue().set(fk, value, timeout, TimeUnit.MILLISECONDS);
    }

    @Override
    public String get(String key) {
        String fk = buildKey(key);
        return template.opsForValue().get(fk);
    }

    public Long getExpireTime(String key) {
        return template.getExpire(key);
    }

    @Override
    public void delete(String key) {
        String fk = buildKey(key);
        template.delete(fk);
    }

    private String buildKey(String scope) {
        return "SECURE_" + support.getGuid() + "_" + scope;
    }
}